

package lolcodecompiler;

import java.util.*;
import java.io.*;
import javax.swing.*;

public class LOLCodeCompiler 
{

    
    public static String input = "";
    public static String token;
    public static String html;
    static MyLexicalAnalyzer lexer;
    static MySyntaxAnalyzer parser;
    static MySemanticAnalyzer converter;
    static ArrayList<String> parseTree;
    static ArrayList<Tags> allTags;
        
    public static void main(String[] args) 
    {
        Scanner reader = null;
        File file = null;

        if(args.length != 1)
        {
            JOptionPane.showMessageDialog(null, "#GIMME ARGS", "Error", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }	
        
        try 
        {
            file = new File(args[0]);
            reader = new Scanner(file);
        } 
        catch (FileNotFoundException e) 
        {
            JOptionPane.showMessageDialog(null, "#GIMME FILEZ", "Error", JOptionPane.ERROR_MESSAGE);
        }

        
        //check extension
        String extension = "";

        String fileName = args[0];

        int i = fileName.lastIndexOf('.');
        if (i > 0) 
        {
            extension = fileName.substring(i+1);
        }

        if(!extension.equals("lol"))
        {
            JOptionPane.showMessageDialog(null, "#GIMME .lol FILE", "Error", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }

        while(reader.hasNext())
        {
            input += reader.nextLine();
        }				
        reader.close();
        

        parseTree = new ArrayList<>();
        allTags = new ArrayList<>();
        populateTags();

        token = "";

        lexer = new MyLexicalAnalyzer();
        lexer.getNextToken();

        parser = new MySyntaxAnalyzer();
        parser.lolcode();

        html = "";

        converter = new MySemanticAnalyzer();
        converter.convertToHtml();


        String filename = args[0];
        filename = filename.substring(0, filename.lastIndexOf("."));
        filename += ".html";

        //write output file
        BufferedWriter writer = null;
        try 
        {
            File logFile = new File(filename);

            writer = new BufferedWriter(new FileWriter(logFile));
            writer.write(html);
        } 
        catch (IOException e) 
        {
        } 
        finally 
        {
            try 
            {
                writer.close();
            } 
            catch (IOException e) 
            {
            }
        }
        JOptionPane.showMessageDialog(null, "#SUCCESS: " + filename + " has been created ", "#SUCCESS", JOptionPane.INFORMATION_MESSAGE);
	}

    private static void populateTags()
    {
        allTags.add(new Tags(LolTokens.DOC_BEGIN, HtmlTokens.DOC_BEGIN));
        allTags.add(new Tags(LolTokens.DOC_END, HtmlTokens.DOC_END));
        allTags.add(new Tags(LolTokens.HEAD_BEGIN, HtmlTokens.HEAD_BEGIN));
        allTags.add(new Tags(LolTokens.HEAD_END, HtmlTokens.HEAD_END ));
        allTags.add(new Tags(LolTokens.COMMENT_BEGIN, HtmlTokens.COMMENT_BEGIN));
        allTags.add(new Tags(LolTokens.COMMENT_END, HtmlTokens.COMMENT_END));
        allTags.add(new Tags(LolTokens.TITLE_BEGIN, HtmlTokens.TITLE_BEGIN));
        allTags.add(new Tags(LolTokens.TITLE_END, HtmlTokens.TITLE_END));
        allTags.add(new Tags(LolTokens.BOLD_BEGIN, HtmlTokens.BOLD_BEGIN));
        allTags.add(new Tags(LolTokens.BOLD_END, HtmlTokens.BOLD_END));
        allTags.add(new Tags(LolTokens.ITALICS_BEGIN, HtmlTokens.ITALICS_BEGIN ));
        allTags.add(new Tags(LolTokens.ITALICS_END, HtmlTokens.ITALICS_END ));
        allTags.add(new Tags(LolTokens.LIST_BEGIN, HtmlTokens.LIST_BEGIN ));
        allTags.add(new Tags(LolTokens.LIST_END, HtmlTokens.LIST_END ));
        allTags.add(new Tags(LolTokens.ITEM_BEGIN, HtmlTokens.ITEM_BEGIN ));
        allTags.add(new Tags(LolTokens.ITEM_END, HtmlTokens.ITEM_END ));
        allTags.add(new Tags(LolTokens.VAR_BEGIN, HtmlTokens.VAR_BEGIN));
        allTags.add(new Tags(LolTokens.VAR_END, HtmlTokens.VAR_END));
        allTags.add(new Tags(LolTokens.USE_BEGIN, HtmlTokens.USE_BEGIN));
        allTags.add(new Tags(LolTokens.USE_END, HtmlTokens.USE_END));
        allTags.add(new Tags(LolTokens.ITS, HtmlTokens.ITS ));
        allTags.add(new Tags(LolTokens.VIDEO_BEGIN, HtmlTokens.VIDEO_BEGIN));
        allTags.add(new Tags(LolTokens.VIDEO_END, HtmlTokens.VIDEO_END));
        allTags.add(new Tags(LolTokens.AUDIO_BEGIN, HtmlTokens.AUDIO_BEGIN));
        allTags.add(new Tags(LolTokens.AUDIO_END, HtmlTokens.AUDIO_END));
        allTags.add(new Tags(LolTokens.PARAGRAPH_BEGIN, HtmlTokens.PARAGRAPH_BEGIN));
        allTags.add(new Tags(LolTokens.PARAGRAPH_END, HtmlTokens.PARAGRAPH_END));
        allTags.add(new Tags(LolTokens.NEWLINE, HtmlTokens.NEWLINE));

    }

}
